package controller;

import com.alibaba.dubbo.config.annotation.Reference;
import entity.ResponseResult;
import entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.UserService;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class UserAction {
    @Reference
    private UserService userService;
    @RequestMapping("register")
    public String register(Users user){
        try {
            String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            user.setCreatetime(nowTime);
            userService.register(user);
            return "注册成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
    }

    /**
     * 删除用户
     * @param id
     */
    @RequestMapping("delete")
    public ResponseResult delete(int id){
        userService.delete(id);
        ResponseResult result = new ResponseResult(true, 200, "删除成功", null);
        return result;

    }

    /**
     * 修改用户
     * @param users
     */
    @RequestMapping("update")
    public ResponseResult update(@RequestBody Users users){
        userService.update(users);
        ResponseResult result = new ResponseResult(true, 200, "修改成功", null);
        return result;
    }

    /**
     * 模糊查询用户
     * @param username
     * @return
     */
    @RequestMapping("findUsersById")
    public ResponseResult findUsersById(String username){
        Users user = userService.findUsersById(username);
        ResponseResult result = new ResponseResult(true, 200, "查询成功",user);
        return result;
    }
}
